package com.auto.autoapp.ui.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.auto.autoapp.R
import com.auto.autoapp.MainActivity
import com.auto.autoapp.base.BaseActivity
import com.auto.autoapp.databinding.ActivityLoginBinding
import com.auto.autoapp.util.SpUtil
import com.google.android.material.button.MaterialButton
import com.google.android.material.textfield.TextInputEditText
import androidx.core.widget.doOnTextChanged
import androidx.lifecycle.lifecycleScope
import com.auto.autoapp.data.api.UserApi
import com.auto.autoapp.data.model.UserInfo
import com.auto.autoapp.network.RetrofitClient
import com.auto.autoapp.network.create
import kotlinx.coroutines.launch
import java.util.UUID
import kotlin.jvm.java
import kotlin.text.isEmpty
import kotlin.text.orEmpty
import kotlin.text.trim

class LoginActivity : BaseActivity() {

    private lateinit var binding: ActivityLoginBinding
    override fun showToolbar(): Boolean {
        return false
    }

    override fun provideView(): View {
        binding = ActivityLoginBinding.inflate(layoutInflater)
        return binding.root
    }


    private fun toMain() {
        startActivity(Intent(this, MainActivity::class.java))
        finish()
        return

    }

    override fun onInit() {
        if (!SpUtil.getToken().isNullOrEmpty()) {
            toMain()
        }
        binding.etAccount.setText(SpUtil.getUserAccount() ?: "")
        // 内容变化时实时更新错误提示
        binding.etAccount.doOnTextChanged { text, _, _, _ ->
            binding.tilAccount.helperText = if (text.isNullOrBlank()) "请输入账号" else null
        }
        binding.etPassword.doOnTextChanged { text, _, _, _ ->
            binding.tilPassword.helperText = if (text.isNullOrBlank()) "请输入密码" else null
        }
        binding.btnLogin.setOnClickListener {
            val account = binding.etAccount.text?.toString()?.trim().orEmpty()
            val password = binding.etPassword.text?.toString()?.trim().orEmpty()

            var hasError = false

            if (account.isEmpty()) {
                binding.tilAccount.helperText = "请输入账号"
                hasError = true
            } else {
                binding.tilAccount.helperText = null
            }

            if (password.isEmpty()) {
                binding.tilPassword.helperText = "请输入密码"
                hasError = true
            } else {
                binding.tilPassword.helperText = null
            }

            if (hasError) return@setOnClickListener

            lifecycleScope.launch {
                val paramMap = mutableMapOf(
                    "username" to account,
                    "password" to password
                )
                val call = RetrofitClient.getApi().login(paramMap)
                RetrofitClient.request({ call }, iView = this@LoginActivity) { res, _ ->
                    if (res != null) {
                        SpUtil.setUserInfo(res)
                        SpUtil.setToken(res.token ?: "")
                        toMain()
                    } else {
                        showToast("获取用户信息失败")
                    }
                }
            }

        }
    }

}